#include "user.h"

User::User(QTcpSocket *socket, QObject *parent) : QObject(parent)
{
    _dataSocket = socket;
    _connectionID = _dataSocket->socketDescriptor();
    _tickSocket = 0;
    _isAuthorize = 0;
    _userID = 0;

    level = 0;
    curentNick = "Guest";
    status = "Undefined";

    Log::add(QString("[User] Create new user [%1, %2]").arg(curentNick,status));
}
User::~User()
{
    if(_dataSocket !=NULL){
        _dataSocket->close();
        _dataSocket = NULL;
    }
    Log::add("[User] User " + curentNick + " deleted");
}


int User::authorize(QString login,QString password)
{
    _userID = DataBase::base->isAutherazation(login,password);
    if(_userID > 0 ){
        _isAuthorize = true;
        level = DataBase::base->getLevel(_userID);
        curentNick = DataBase::base->getName(_userID);
        status = DataBase::base->getStatus(_userID);
        Log::add(QString("[User] User %1 AUTHORIZE").arg(curentNick));
    }
    return _userID;
}
